// OSR B CmdLine
package org.jvcs.common;

import java.io.*;
import java.util.*;

public class CmdLine {

	public static int argi = 0;
	public static int argc = 0;
	public static String [] gargs = null;
	public static String mesg = "";
	public static ArrayList <String> optAL = new ArrayList<String>();
	public static ArrayList <String> argAL = new ArrayList<String>();
	public static String cmd = "";
	public static boolean verboseFlag = false;
	public static boolean commentFlag = false;
	public static boolean versionFlag = false;
	public static boolean helpFlag = false;
	public static boolean helpCmdFlag = false;
	public static boolean optionSetFlag = false;
	public static int optSetCnt = 0;
	public static String helpCmd = "";
	
	// For convenience explicit declarations of flags
	public static boolean includeFlag = false;
	public static boolean excludeFlag = false;
	public static boolean contextFlag = false;
	public static boolean forceFlag = false;
	public static boolean iFlag = false;
	public static String includeOptSet = null;
	public static String excludeOptSet = null;
	public static String contextOptSet = null;
	public static String contextValue = null;
	
	public static String comments = "";
	public static String currArg = "";
	



///////////////////////////////////////////////////////////////////////////////
public static boolean isKV(String in) {
	String [] tmp = in.split("=");
	if (tmp.length == 2) {
		return true;
	}
	return false;
}
public static String [] getKV(String in) {
	String [] tmp = in.split("=");
	if (tmp.length == 2) {
		return tmp;
	}
	String [] t = {};
	return t;
}
public static boolean isSeq(String in) {
	String k = null;
	String v = null;
	String [] t = null;
	
	if (isKV(in)) {
		t = getKV(in);
		k = t[0];
		v = t[1];
		if (verboseFlag) osr.pl("isSeq: key/value: "+k+" ==> "+v);
	}
	if ((v != null) && (v.indexOf(",") != -1))
		return true;
	else
		return false;
}
public static String [] getSeq(String in) {
	String [] t = null;
	if(isSeq(in)) {
		String [] a = getKV(in);
		t = a[1].split(",");
	}
	for(int i=0;i<t.length;i++) {
		String t1 = t[i].trim();
		if (verboseFlag) osr.pl("Each WC => "+t1);
		t[i] = t1;
	}
	return t;
} 
public static String getValue(String in) {
	String k = null;
	String v = null;
	String [] t = null;
	
	if (isKV(in)) {
		t = getKV(in);
		k = t[0];
		v = t[1];
		if (verboseFlag) osr.pl("getValue: key/value: "+k+" ==> "+v);
	}
	if ((v != null))
		return v;
	else
		return v;
}
public static void parse(String [] args) {
	gargs = args;
	// parse the argument array
	argc = args.length;
	int optCnt = 0;
	int saCnt = 0;
	if (argc >= 1) {
		cmd = args[0];
		cmd = cmd.toLowerCase();
		if (cmd.startsWith("-")) {
			cmd = "";
		}	
	}
	
	while(argi < argc) {
		
		currArg = args[argi].toLowerCase();
		//osr.pl("CurrARG: "+currArg+ " count "+argi + " total "+args.length);
		if (currArg.startsWith("-m") || currArg.startsWith("--message") || currArg.startsWith("-message") || currArg.startsWith("--m")) {
			commentFlag = true;
			if (argi < (argc - 1)) {
				comments = args[++argi];
			}
			optSetCnt++;
		}
		if (currArg.startsWith("-ver") || currArg.startsWith("--version") || currArg.startsWith("--ver")) {
			versionFlag = true;
		}
		if (currArg.equalsIgnoreCase("-v") || currArg.equalsIgnoreCase("--v")) {
			verboseFlag = true;
		}
		if (currArg.equals("-i") || currArg.equalsIgnoreCase("--i")) {
			iFlag = true;
		}
		if (currArg.startsWith("-h") || currArg.startsWith("--h") || currArg.startsWith("--help") || currArg.startsWith("?")) {
			helpFlag = true;
			if (argi < (argc-1)) {
				helpCmd = args[++argi].toLowerCase();
				helpCmdFlag = true;
			}
		}
		if ( (!commentFlag || !helpFlag || !versionFlag || !verboseFlag) ) {
			if ( currArg.startsWith("-") || currArg.startsWith("--")) {
				String optSet = args[argi];
				optAL.add(optCnt++, optSet);
				optSetCnt++;
				if (currArg.startsWith("-include") || currArg.startsWith("--include")) {
					includeFlag = true;		
					includeOptSet = optSet;
				}
				if (currArg.startsWith("-exclude") || currArg.startsWith("--exclude")) {
					excludeFlag = true;
					excludeOptSet = optSet;
				}
				if (currArg.startsWith("-context") || currArg.startsWith("--context")) {
					contextFlag = true;
					contextOptSet = optSet;
					if ((contextOptSet != null) && (isKV(contextOptSet)) ) {
						contextValue = getValue(contextOptSet);
					}
				}
				if (currArg.startsWith("-f") || currArg.startsWith("--f") || currArg.startsWith("-force") || currArg.startsWith("--force")) {
					forceFlag = true;
				}
			} else {
				argAL.add(saCnt++, args[argi]);
			}
		}
	
		argi++;
	}
	// correct the command and args
	if (argAL.size() > 0) {
		cmd = argAL.get(0);
		argAL.remove(0);
	}
		

}
///////////////////////////////////////////////////////////////////////////////

}